package com.rafahead.bloquinho;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.rafahead.bloquinho.dao.PlayerDAO;
import com.rafahead.bloquinho.model.Player;

public class PlayerEditFragment extends Fragment {
	
	private PlayerDAO mPlayerDAO;
	
	private EditText mPlayerName;
	
	private Long mPlayerID;

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);

		mPlayerID = (savedInstanceState == null) ? null :(Long) savedInstanceState.getSerializable(PlayerDAO.COLUMN_ID);
		if (mPlayerID == null) {
			Bundle extras = getActivity().getIntent().getExtras();
			mPlayerID = extras != null ? extras.getLong(PlayerDAO.COLUMN_ID): null;
		}

        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.player_edit,container, false);
        //mPlayerName = (EditText) getActivity().findViewById(R.id.playerEditName);
        return view;
	}
	
    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
        	mPlayerID = args.getLong(PlayerDAO.COLUMN_ID);
        	populateFields();
        } 
    }

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.player_edit, menu);
		super.onCreateOptionsMenu(menu,inflater);		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menuPlayerSave:
			saveState();
			InputMethodManager inputManager =(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
			inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 
			getActivity().getSupportFragmentManager().popBackStack();
			break;

		default:
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onPause() {
		super.onPause();
		saveState();
		mPlayerDAO.close();
	}

	@Override
	public void onResume() {
		super.onResume();
		populateFields();
	}


	//fill the form
	private void populateFields(){
		if (mPlayerID!=null){
			mPlayerDAO = new PlayerDAO(getActivity());
			mPlayerDAO.open();
			
			Player player = mPlayerDAO.searchPlayer(mPlayerID);
			
			
			mPlayerName = (EditText) getActivity().findViewById(R.id.playerEditName);
			mPlayerName.setText(player.getName());
			
		}
	}
	
	//create a player
	private void saveState() {
		
		mPlayerName = (EditText) getActivity().findViewById(R.id.playerEditName);
		
		mPlayerDAO = new PlayerDAO(getActivity());
		mPlayerDAO.open();
		
		
		String name = mPlayerName.getText().toString();
		if (mPlayerID == null){
			long id = mPlayerDAO.insertPlayer(name);
			if (id > 0){
				mPlayerID=id;
			}
		}else{
			mPlayerDAO.updatePlayer(mPlayerID, name);
		}
		if (name.trim().compareTo("")==0){
			mPlayerDAO.updatePlayer(mPlayerID, "Player_" + Long.valueOf(mPlayerID).toString());
			
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(PlayerDAO.COLUMN_ID, mPlayerID);
	}
}
